package com.dell.chen.servlet;

import com.dell.chen.pojo.Sort;
import com.dell.chen.service.Impl.SortServiceImpl;
import com.dell.chen.service.SortService;
import com.dell.chen.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ：ChenwithIU
 * @date ：Created in 2020/12/2 9:38
 */
public class SortServlet extends BaseServlet{

    private SortService sortService = new SortServiceImpl();

    protected void addSort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id1 = req.getParameter("id");
        int id = WebUtils.parseInt(id1, 0);

        String typeName = req.getParameter("typeName");

//        List<Sort> sorts = sortService.querySort();
//        if (id==sort)

        //后台添加花品种类  封装Sort类
        Sort sort = WebUtils.copyParamToBean(req.getParameterMap(), new Sort(id,typeName));
        //调用sortService.addSrot方法
        sortService.addSort(sort);

        req.getRequestDispatcher("/manager/goodsType_list.jsp").forward(req,resp);

    }

    /**
     * 获取sort_id 对应的内容 渲染到 后台 manager_add 下的  分类
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getSortId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Sort> sorts = sortService.querySort();

        req.getSession().setAttribute("sorts",sorts);

        req.getRequestDispatcher("/manager/goods_add.jsp").forward(req,resp);
    }


    /**
     * 渲染 goodsType_list 列表内容
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Sort> sorts = sortService.querySort();

        //调用Gson  将  list 转换为  Json 数据   进行渲染
        Gson gson = new Gson();
        resp.getWriter().write(gson.toJson(sorts));
    }

}
